import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/live/live_gift_entity.dart';
import 'package:ball_lion/server/api/live_api.dart';

import 'package:ball_lion/entity/live/live_board_entity.dart';
import 'package:ball_lion/utils/user.dart';
import 'package:ball_lion/utils/utils.dart';

class LiveGiftLogic extends GetxController
    with GetSingleTickerProviderStateMixin {
  int? _liveId;
  final ScrollController scrollController = ScrollController();
  late final TabController tabController =
      TabController(length: 2, vsync: this);
  bool _showTopShader = false;
  bool _showBottomShader = true;
  bool _showNumList = false;
  int? _giftIndex;
  Timer? topsTimer;
  List<GiftEntity>? gifts;
  List<BoardEntity>? tops;
  late BoardEntity userBoard = BoardEntity.fromUser();
  int? userRank;

  bool get showTopShader => _showTopShader;
  bool get showBottomShader => _showBottomShader;
  bool get showNumList => _showNumList;
  int? get giftIndex => _giftIndex;
  int? get liveId => _liveId;

  set liveId(int? value) {
    _liveId = value;
    update();
  }

  set giftIndex(int? value) {
    _giftIndex = value;
    update();
  }

  set showNumList(bool value) {
    _showNumList = value;
    update();
  }

  set showBottomShader(bool value) {
    _showBottomShader = value;
    update();
  }

  set showTopShader(bool value) {
    _showTopShader = value;
    update();
  }

  @override
  void onInit() {
    scrollController.addListener(() {
      if (scrollController.offset >= 0 && showTopShader == false) {
        showTopShader = true;
      } else if (scrollController.offset <= 0 && showTopShader == true) {
        showTopShader = false;
      }
      if (scrollController.position.pixels <=
              scrollController.position.maxScrollExtent - 10 &&
          showBottomShader == false) {
        showBottomShader = true;
      } else if (scrollController.position.pixels >=
              scrollController.position.maxScrollExtent - 10 &&
          showBottomShader) {
        showBottomShader = false;
      }
    });
    super.onInit();
  }

  @override
  void onReady() {
    getGifts();
    getTops();
    topsTimer = Timer.periodic(const Duration(seconds: 5), (timer) {
      getTops();
    });
    super.onReady();
  }

  Future getGifts() async {
    gifts = await LiveApi.getLiveGift(liveId);
    update();
  }

  Future getTops() async {
    tops = await LiveApi.getLiveBoard(liveId);
    userBoard.roomId = _liveId;
    if (tops != null) {
      for (var i = 0; i < tops!.length; i++) {
        if (tops![i].userId == userBoard.userId) {
          userBoard.totalGold = tops![i].totalGold;
          userRank = i;
          break;
        }
      }
    }
    update();
  }

  void showGiftNumList(int index) {
    if (giftIndex != index) {
      giftIndex = index;
    }
    showNumList = true;
  }

  void selectGift(int index) {
    if (giftIndex != index) {
      giftIndex = index;
    }
    if (showNumList) showNumList = false;
  }

  Future payGift(int index) async {
    if (showNumList) return;
    var result = await LiveApi.sendGift(liveId!, gifts![index].id!, 1);
    if (result == 200) {
      Utils.onEvent("liveroom_gift_give",
          params: {"liveroom_gift_give": "${gifts![index].id}"});
      User.fetchUserInfo(fetchFocus: false);
      update();
      navigator?.pop();
    }
  }

  Future payGiftNum(GiftEntity entity, int numIndex) async {
    var result =
        await LiveApi.sendGift(liveId!, entity.id!, entity.giftUnit![numIndex]);
    if (result == 200) {
      User.fetchUserInfo(fetchFocus: false);
      update();
      navigator?.pop();
    }
  }

  @override
  void onClose() {
    topsTimer?.cancel();
    super.onClose();
  }
}
